package com.ly.dingTalk.service.impl;

import static com.ly.dingTalk.constant.DtApiPathConstant.User.UNION_ID_2_USER_ID;
import static com.ly.dingTalk.constant.DtApiPathConstant.User.USER_GET;
import static com.ly.dingTalk.constant.DtApiPathConstant.User.USER_GET_V2;
import static com.ly.dingTalk.constant.DtApiPathConstant.User.USER_ID_GET;
import static com.ly.dingTalk.constant.DtApiPathConstant.User.USER_ID_LIST;
import static com.ly.dingTalk.constant.DtApiPathConstant.User.USER_LIST;
import static com.ly.dingTalk.constant.DtApiPathConstant.User.USER_SIMPLE_LIST;

import java.util.List;

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ly.dingTalk.bean.user.DtUnionId2UserId;
import com.ly.dingTalk.bean.user.DtUser;
import com.ly.dingTalk.bean.user.DtUserV2;
import com.ly.dingTalk.constant.DtApiPathConstant;
import com.ly.dingTalk.dingcon.DtService;
import com.ly.dingTalk.error.DtErrorException;
import com.ly.dingTalk.service.DtUserService;
import com.ly.dingTalk.util.json.GsonHelper;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RequiredArgsConstructor
@Slf4j
public class DtUserServiceImpl implements DtUserService {
    private final DtService mainService;

    @Override
    public DtUser getById(String userId) throws DtErrorException {
        String url = this.mainService.getDtConfigStorage().getApiUrl(USER_GET + userId);
        String responseContent = this.mainService.get(url, null);
        return DtUser.fromJson(responseContent);
    }
    
    @Override
    public DtUserV2 getV2ById(String userId) throws DtErrorException {
        String url = this.mainService.getDtConfigStorage().getApiUrl(USER_GET_V2);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("userid", userId);
        jsonObject.addProperty("language", "zh_CN");
        String responseContent = this.mainService.post(url, jsonObject);
        log.info("v2 用户信息: {}",responseContent);
        JsonObject asJsonObject = GsonHelper.parse(responseContent).getAsJsonObject("result");
        return DtUserV2.fromJson(asJsonObject.toString());
    }

    @Override
    public String getUserIdByMobile(String mobile) throws DtErrorException {
        String url = this.mainService.getDtConfigStorage().getApiUrl(USER_ID_GET + mobile);
        String responseContent = this.mainService.get(url, null);
        JsonObject jsonObject = GsonHelper.parse(responseContent);
        return GsonHelper.getString(jsonObject, "userid");
    }

    @Override
    public List<String> userIdsByDepartment(Long departId) throws DtErrorException {
        String url = this.mainService.getDtConfigStorage().getApiUrl(USER_ID_LIST + departId);
        String responseContent = this.mainService.get(url, null);
        JsonObject jsonObject = GsonHelper.parse(responseContent);
        return GsonHelper.create()
                .fromJson(
                        jsonObject.get("userIds"),
                        new TypeToken<List<Long>>() {}.getType()
                );
    }

    @Override
    public List<DtUser> listByDepartment(Long departId, Integer offset, Integer size, String order) throws DtErrorException {
        String params = "";
        if (order != null) {
            params += "&order=" + order;
        }

        String url = String.format(this.mainService.getDtConfigStorage().getApiUrl(USER_LIST), departId, offset, size);
        String responseContent = this.mainService.get(url, params);
        JsonObject jsonObject = GsonHelper.parse(responseContent);
        return GsonHelper.create()
                .fromJson(
                        jsonObject.get("userlist"),
                        new TypeToken<List<DtUser>>() {}.getType()
                );
    }

    @Override
    public List<DtUser> listSimpleByDepartment(Long departId, Integer offset, Integer size, String order) throws DtErrorException {
        String params = "";
        if (order != null) {
            params += "&order=" + order;
        }

        String url = String.format(this.mainService.getDtConfigStorage().getApiUrl(USER_SIMPLE_LIST), departId, offset, size);
        String responseContent = this.mainService.get(url, params);
        JsonObject tmpJson = GsonHelper.parse(responseContent);
        return GsonHelper.create()
                .fromJson(
                        tmpJson.get("userlist"),
                        new TypeToken<List<DtUser>>() {}.getType()
                );
    }

    @Override
    public DtUnionId2UserId unionId2UserId(String unionId) throws DtErrorException {
        String url = this.mainService.getDtConfigStorage().getApiUrl(UNION_ID_2_USER_ID + unionId);
        String responseContent = this.mainService.get(url, null);
        return DtUnionId2UserId.fromJson(responseContent);
    }

    @Override
    public String createUserV2(DtUserV2 userV2) throws DtErrorException {
        String url = this.mainService.getDtConfigStorage().getApiUrl(DtApiPathConstant.User.USER_CREATE_V2);
        String responseContent = this.mainService.post(url, userV2.toJson());
        return responseContent;
    }
}
